package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsHomeBrandServiceApi;
import com.itheima.hmshopping.dubbo.mapper.SmsHomeBrandMapper;
import com.itheima.hmshopping.entity.SmsHomeBrand;
import com.itheima.hmshopping.entity.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeBrandServiceApiImpi implements SmsHomeBrandServiceApi {
    @Autowired
    private SmsHomeBrandMapper smsHomeBrandMapper;

    //添加首页新品
    @Override
    public void insert(SmsHomeBrand smsHomeBrand) {
        smsHomeBrandMapper.insert(smsHomeBrand);
    }

    //批量删除首页新品
    @Override
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<SmsHomeBrand> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ObjectUtil.isNotEmpty(ids),SmsHomeBrand::getBrandId,ids);
        smsHomeBrandMapper.delete(lambdaQueryWrapper);
    }
    //分页查询首页新品
    @Override
    public Page<SmsHomeBrand> page(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeBrand> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeBrand> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //介绍
        if (StrUtil.isNotBlank(productName)){
            lambdaQueryWrapper.like(SmsHomeBrand::getBrandName,productName);
        }
        //状态
        if (ObjectUtil.isNotEmpty(recommendStatus)){
            lambdaQueryWrapper.eq(SmsHomeBrand::getRecommendStatus,recommendStatus);
        }
        lambdaQueryWrapper.orderByAsc(SmsHomeBrand::getSort);
        Page<SmsHomeBrand> smsHomeBrandPage = smsHomeBrandMapper.selectPage(page, lambdaQueryWrapper);
        return smsHomeBrandPage;
    }
    //批量修改首页新品状态
    @Override
    public void updateList(List<Long> ids, Integer recommendStatus) {
        //查询所有产品
        LambdaQueryWrapper<SmsHomeBrand> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ObjectUtil.isNotEmpty(ids),SmsHomeBrand::getId,ids);
        List<SmsHomeBrand> smsHomeBrands = smsHomeBrandMapper.selectList(lambdaQueryWrapper);
        //批量修改状态
        for (SmsHomeBrand smsHomeBrand : smsHomeBrands) {
            smsHomeBrand.setRecommendStatus(recommendStatus);
            smsHomeBrandMapper.updateById(smsHomeBrand);
        }
    }
    //修改首页新品排序
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setSort(sort);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }
}
